package com.wlz.splitpackage.customlength;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wlz
 * @date 2022-07-09  3:51 下午
 */
@Slf4j
public class CLengthPackageNettyClient {

    public static void main(String[] args) {

        EventLoopGroup group = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();

            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pipeline = socketChannel.pipeline();

                            pipeline.addLast(new MyMessageEncoder());
                            pipeline.addLast(new CLengthPackageNettyClientHandler());
                        }
                    });

            log.info("netty client start .....");

            ChannelFuture cf = bootstrap.connect("127.0.0.1", 9000).sync();

            log.info(" =========   {}  ==========", cf.channel().localAddress());

            cf.channel().closeFuture().sync();

        } catch (Exception e) {
            log.error("CLengthPackageNettyClient error : {}", e);
        } finally {
            group.shutdownGracefully();
        }


    }
}
